package com.springboot.service.impl;

import com.springboot.mapper.EmployeeMapper;
import com.springboot.model.Employee;
import com.springboot.model.EmployeeInfo;
import com.springboot.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: TongRui乀
 * @Date: 2019-11-15 15:54
 * @description：
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public List<Employee> getEmployee() {
        return this.employeeMapper.getEmployee();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createEmployee(String empName, String desc, Integer deptId) {

        Employee employee = new Employee();

        employee.setId(2);
        employee.setName(empName);
        employee.setDeptId(deptId);

        EmployeeInfo employeeInfo = new EmployeeInfo();

        employeeInfo.setId(2);
        employeeInfo.setDesc(desc);
        employeeInfo.setEmployeeId(employee.getId());

        this.employeeMapper.insert(employee);

        this.employeeMapper.insertInfo(employeeInfo);

    }

    @Transactional
    @Override
    public String testTx(Integer isException) {

        List<Employee> employees = this.employeeMapper.getEmployee();

        if(employees.size() > 0){

            Employee employee = employees.get(0);

            employee.setName(employee.getName() + "1");

            this.employeeMapper.update(employee);
        }

        if(isException == 0){
            throw new RuntimeException("测试事务失败");
        }else{
            return "SUCCESS";
        }

    }
}
